package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Dispensing;
import com.zhiyou100.service.DispensService;

@Controller
public class DispensController {
	@Autowired
	private DispensService dispensService;
	
	@RequestMapping("/dispensing/list")
	public String dispensList(@RequestParam(defaultValue="")String medicalRecord,
			@RequestParam(defaultValue="1")int pageNo,Model model) {
		//模糊查询
		Map<String,Object> map = new HashMap<>();
		map.put("medicalRecord", medicalRecord);
		// 先声明分页再调用服务层
		PageHelper.startPage(pageNo, 3);
		System.out.println("发药模块进入controller");
		List<Dispensing> dispensing = dispensService.findAll(map);
		PageInfo<Dispensing> pageInfo = new PageInfo<>(dispensing);
		model.addAttribute("map", map);
		model.addAttribute("page", pageInfo);
		model.addAttribute("dispensing", pageInfo.getList());
		return "forward:/view/hospital/dispensingList.jsp";
//		return "forward:/success.jsp";
	}
	
	// 详情
	@RequestMapping("/dispensing/detail")
	public String detail(int medical_record,Model model,
			@RequestParam(defaultValue="1")int pageNo) {
		System.out.println("进入发药详情controller");
		PageHelper.startPage(pageNo, 3);
		List<Dispensing> dis = dispensService.findById(medical_record);
		PageInfo<Dispensing> pageInfo = new PageInfo<>(dis);
		model.addAttribute("medical_record",medical_record );
		model.addAttribute("dis", pageInfo.getList());
		model.addAttribute("page", pageInfo);
		return "forward:/view/hospital/dispensingDetail.jsp";
		
	}
	// 详情内操作
	@RequestMapping("/dispensing/update")
	public String update(int send_num,String drug_num) {
		Map<String,Object> map = new HashMap<>();
		map.put("send_num", send_num);
		map.put("drug_num", drug_num);
		if(send_num==1) {
			System.out.println("详情页面内发药1: "+send_num);
			dispensService.updateSendOne(map);
		}else if(send_num==5) {
			System.out.println("详情页面内发药5: "+send_num);
			dispensService.updateSendFive(map);
		}else if(send_num==30) {
			System.out.println("详情页面内发药30: "+send_num);
			dispensService.updateSendThirty(map);
		}else {
			System.out.println("详情页面内发药全部: "+send_num);
			dispensService.updateSendAll(map);
		}
		System.out.println("已发完药");
		return "forward:/dispensing/list";
	}
	// 发药
	@RequestMapping(value="/dispensing/add",method=RequestMethod.GET)
	public  String dispensindAdd(int medical_record,Model model) {
		Dispensing dis = dispensService.findToAdd(medical_record);
		model.addAttribute("dis", dis);
		return "forward:/view/hospital/dispensingAdd.jsp";
		 
	}
	@RequestMapping(value="/dispensing/add",method=RequestMethod.POST)
	public  String dispensindAdd(Dispensing dis) {
		dispensService.addDispensing(dis);
		return "forward:/dispensing/list";
		 
	}
	
}
